package com.ygo.feihua.Management;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import com.ygo.feihua.BmobTable.GroupChatMessage;
import com.ygo.feihua.BmobTable.MyUser;
import com.ygo.feihua.Gongju;
import com.ygo.feihua.LiaotianQunliao;
import com.ygo.feihua.R;
import java.util.List;
import android.view.LayoutInflater;

import android.text.Html.ImageGetter;
import android.widget.LinearLayout;
import cn.bmob.v3.datatype.BmobGeoPoint;
import android.graphics.Color;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import com.ygo.feihua.YonghuMingpian;
import cn.bmob.v3.datatype.BmobFile;
import android.util.Log;
import android.text.Spanned;
import android.text.SpannedString;
import android.net.Uri;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.*;
import android.text.style.*;
import android.widget.*;
import com.ygo.feihua.*;
import android.os.*;
import android.support.v4.util.*;
import java.util.*;
import android.view.animation.*;
import com.ygo.feihua.util.*;
import com.ygo.feihua.view.*;
import android.support.v7.widget.*;
import android.support.design.widget.FloatingActionButton;
import java.text.*;
import com.msk86.ygoroid.newutils.*;

public class LiaotianView implements LiaotianListener,OnClickListener
{
	
	private Context context;
	//private ListView lt_list;
	private RecyclerView lt_rec;
	private CardView lt_send;
	private EditText lt_send_message;
	
	private ProgressBar lt_jz;
	
	private boolean fsing=true;
	
	private Gongju gj;
	private DialogUtils du;
	private UserManagement um;
	private LiaotianManagement ltm;
	LinearLayoutManager llm;
	
	private List<Map<String,Object>> data;
	private LinearLayout lt_bottom_bar;
	private AdapterLt adplt;
	
	int le=0;

	private View ltView;
	
	public LiaotianView(final Context context,ViewGroup v){
		
		this.context=context;
		
		ltView=LayoutInflater.from(context).inflate(R.layout.liaotian_view,v,false);
		
		this.lt_rec=(RecyclerView) ltView.findViewById(R.id.lt_rec);
		this.lt_send_message=(EditText) ltView.findViewById(R.id.lt_send_message);
		this.lt_send=(CardView) ltView.findViewById(R.id.lt_send);
		this.lt_jz=(ProgressBar) ltView.findViewById(R.id.lt_jz);
		this.lt_bottom_bar=(LinearLayout) ltView.findViewById(R.id.lt_bottom_bar);
		
		v.addView(ltView);
		
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
		um=UserManagement.getDx();
		ltm=LiaotianManagement.getdx();
		llm=new LinearLayoutManager(context);
		
		lt_rec.setLayoutManager(llm); 
		
		ltm.setLtl(this);
		data=ltm.getSl();
		//lt_rec.setLayoutManager(llm);
		
		
		if(data.size()==0){
		lt_bottom_bar.setVisibility(View.GONE);
		}
		lt_send.setOnClickListener(this);
		
		adplt=new AdapterLt();

		lt_rec.setAdapter(adplt);
		lt_rec.smoothScrollToPosition(data.size());
		
		//((LinearLayoutManager)lt_rec.getLayoutManager()).scrollToPositionWithOffset(data.size()-1,0);
	}

	@Override
	public void jsMessageException(Exception e)
	{
		// TODO: Implement this method
	}
	
	@Override
	public void listenerException(Exception e)
	{
		
		// TODO: Implement this method
	}
	
	@Override
	public void adddel(int qk, Map<String, Object> se, int delpo)
	{
	
		switch(qk){
			case LiaotianManagement.MESSAGE_TYPE_ADD:
				adplt.sx();
				lt_rec.smoothScrollToPosition(delpo);
				//((LinearLayoutManager)lt_rec.getLayoutManager()).scrollToPositionWithOffset(delpo,0);
				break;
			case LiaotianManagement.MESSAGE_TYPE_DEL:
				
				break;
		}
		
		// TODO: Implement this method
	}

	@Override
	public void ltsx(List<Map<String, Object>> se)
	{
		// TODO: Implement this method
	}

	
	
	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.lt_send:
				send();
				break;
		}
		
		// TODO: Implement this method
	}
	
	private void send()
	{
		final String string =lt_send_message.getText().toString().trim();
		if (um.getUser() == null){
			gj.show("还没登录哟~");
			um.logInUser(context);
		}else{
			if (string.equals("")){
				gj.show("发送信息不能为空");
			}else{
				if (gj.isfs(string)){
					if (fsing){
						sendMessage(string);
					}
				} else
				{
					gj.show("发送内容包含敏感词");
				}
			}
		}

	}

	private void sendMessage(final String message)
	{
		fsing=false;

		lt_send.setVisibility(View.GONE);
		lt_jz.setVisibility(View.VISIBLE);
		BmobQuery<MyUser> query = new BmobQuery<MyUser>();
		query.getObject(um.getUser().getObjectId(), new QueryListener<MyUser>() {

				@Override
				public void done(MyUser p1, BmobException p2)
				{
					if(p2==null){
						if(p1.getLiaotian_qx()!=1){

							new GroupChatMessage(um.getUser(), um.getUser().getUsername(), message, "发送版本"+Utils.s(R.string.app_name)).save( new SaveListener<String>() {

									@Override
									public void done(String p1, BmobException p2)
									{
										if(p2==null){
											lt_send_message.getText().clear();
										}else{
											gj.show("发送失败,原因为"+p2);
										}
										lt_send.setVisibility(View.VISIBLE);
										lt_jz.setVisibility(View.GONE);
										fsing=true;
										// TODO: Implement this method
									}
								});

						}else{
							lt_send.setVisibility(View.VISIBLE);
							lt_jz.setVisibility(View.GONE);
							gj.show("喵被禁言了呢QAQ");
							fsing=true;
						}
					}else{
						lt_send.setVisibility(View.VISIBLE);
						lt_jz.setVisibility(View.GONE);
						gj.show("发送失败,原因为"+p2);
						fsing=true;
					}
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
	}
	
	public void sxadp(){
		adplt.sx();
		lt_rec.scrollToPosition(data.size()-1);
		//((LinearLayoutManager)lt_rec.getLayoutManager()).scrollToPositionWithOffset(data.size()-1,0);
		lt_bottom_bar.setVisibility(View.VISIBLE);
		lt_bottom_bar.startAnimation(AnimationUtils.loadAnimation(context,R.anim.in_from_down));
	}
	
	private class AdapterLt extends RecyclerView.Adapter<AdapterLt.ViewHolder>
	{

		@Override
		public LiaotianView.AdapterLt.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
		{
			View v=LayoutInflater.from(p1.getContext()).inflate(R.layout.chat_item, p1,false);
			ViewHolder viewHolder=new ViewHolder(v);
			// TODO: Implement this method
			return viewHolder;
		}
		

		@Override
		public void onBindViewHolder(LiaotianView.AdapterLt.ViewHolder p1, final int position)
		{
			
			p1.textview_sj.setText((String)data.get(position).get("time"));
			p1.textView_name.setText((String)data.get(position).get("name"));
			if (um.getUser() != null && um.getUser().getUserpoint() != null){
				BmobGeoPoint gepo=(BmobGeoPoint) data.get(position).get("point");
				if (gepo != null){
					if (um.getUser().getUserpoint().getLongitude() != 0 && um.getUser().getUserpoint().getLatitude() != 0 && gepo.getLatitude() != 0 && gepo.getLongitude() != 0){
						p1.textview_jl.setVisibility(View.VISIBLE);
						p1.textview_jl.setText(gj.formatDouble(gepo.distanceInKilometersTo(um.getUser().getUserpoint()), 2) + "KM");
					}else{
						p1.textview_jl.setVisibility(View.GONE);
					}
				}else{
					p1.textview_jl.setVisibility(View.GONE);
				}
			}else{
				p1.textview_jl.setVisibility(View.GONE);
			}
			
			gj.setTouxiang(p1.icon,(MyUser)data.get(position).get("user"));
			
			p1.lm.setMessage((String)data.get(position).get("neirong"));

			switch((int)data.get(position).get("userqx")){
				case 1:
					p1.textView_name.setTextColor(Color.RED);
					break;
				default:
					p1.textView_name.setTextColor(Color.BLACK);
			}
			p1.lt_dialog.setOnLongClickListener(new OnLongClickListener(){

					@Override
					public boolean onLongClick(View p1)
					{
						ListView v3=(ListView) du.dialogl(false,"",new String[]{"复制","删除"});
						v3.setOnItemClickListener(new OnItemClickListener(){

								@Override
								public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
								{
									switch(p3){
										case 0:
											gj.fzMessage((String)data.get(position).get("neirong"));
											gj.show("已复制到剪贴板");
											break;
										case 1:
											data.remove(position);
											notifyDataSetChanged();
											gj.show("删除成功");
											break;
									}
									du.dis();
									// TODO: Implement this method
								}
							});
						// TODO: Implement this method
						return true;
					}
				});

			p1.icon.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						if((String)data.get(position).get("id")!=null){
							Intent intent=new Intent(context,YonghuMingpian.class);
							intent.putExtra("id",(String)data.get(position).get("id"));
							context.startActivity(intent);
						}else{
							gj.show("没有该用户信息");
						}
						// TODO: Implement this method
					}
				});		
			p1.vh.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						gj.closeKeyboard();
						// TODO: Implement this method
					}
				});
			// TODO: Implement this method
		}

		
		
		
		@Override
		public int getItemCount()
		{
			// TODO: Implement this method
			return data.size();
		}

		
		public AdapterLt(){
		
		}
		
		private class ViewHolder extends RecyclerView.ViewHolder
		{
			TextView textView_name,textview_sj,textview_jl;
			ImageView icon;
			LinearLayout lt_dialog;
			LiaotianMessageLayout lm;

			View vh;
			
			public ViewHolder(View v){
				super(v);
				vh=v;
				icon = (ImageView) v.findViewById(R.id.icon);
				textView_name = (TextView) v.findViewById(R.id.textView_name);
				lt_dialog = (LinearLayout) v.findViewById(R.id.lt_dialog);
				textview_sj = (TextView) v.findViewById(R.id.textView_sj);
				textview_jl = (TextView) v.findViewById(R.id.textview_jl);		
				lm = (LiaotianMessageLayout) v.findViewById(R.id.lm);
			}
		}
		
		public void sx(){
			notifyDataSetChanged();
		}
		
	}
	
}
